-
Notifications
You must be signed in to change notification settings - Fork 198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change Setup Wizard redirect to work on all relevant pages #7581
Conversation
Test the previous changes of this PR with WordPress Playground. |
@@ -81,4 +81,13 @@ | |||
<exclude-pattern>**/views/*</exclude-pattern> | |||
<exclude-pattern>tests/bootstrap.php</exclude-pattern> | |||
</rule> | |||
|
|||
<rule ref="WordPress.WP.Capabilities"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It adds the Sensei custom capability, so PHPCS issues don't happen when it's used.
@@ -446,7 +448,7 @@ public function get_woocommerce_connect_data() { | |||
* | |||
* @return stdClass Extension with status. | |||
*/ | |||
private function get_feature_with_status( $extension, $installing_plugins, $selected_plugins ) { | |||
private function get_feature_with_status( $extension, $installing_plugins, $selected_plugins ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- Called by a public deprecated method. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a PHPCS fix.
@@ -545,7 +547,7 @@ public static function close_wccom_install() { | |||
|
|||
if ( | |||
isset( $_SERVER['HTTP_REFERER'] ) && | |||
0 === strpos( $_SERVER['HTTP_REFERER'], 'https://woocommerce.com/checkout' ) && // phpcs:ignore sanitization ok. | |||
0 === strpos( $_SERVER['HTTP_REFERER'], 'https://woocommerce.com/checkout' ) && // phpcs:ignore -- sanitization ok. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a PHPCS fix.
@@ -496,7 +498,7 @@ public function get_sensei_extensions( $clear_active_plugins_cache = false ) { | |||
} | |||
|
|||
$extensions = array_map( | |||
function( $extension ) use ( $installing_plugins, $selected_plugins ) { | |||
function ( $extension ) use ( $installing_plugins, $selected_plugins ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a PHPCS fix.
@@ -93,7 +93,7 @@ public function __construct() { | |||
add_action( 'current_screen', [ $this, 'remove_notices_from_setup_wizard' ] ); | |||
add_action( 'admin_notices', [ $this, 'setup_wizard_notice' ] ); | |||
add_action( 'admin_init', [ $this, 'skip_setup_wizard' ] ); | |||
add_action( 'admin_init', [ $this, 'activation_redirect' ] ); | |||
add_action( 'current_screen', [ $this, 'activation_redirect' ] ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's changed because now we want to use the get_current_screen
, and it's not ready in the admin_init
hook.
@@ -221,6 +220,8 @@ protected function redirect_to_setup_wizard() { | |||
* Render app container for setup wizard. | |||
*/ | |||
public function render_wizard_page() { | |||
// Delete option when the Setup Wizard is loaded, so it doesn't redirect anymore. | |||
delete_option( 'sensei_activation_redirect' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved from the redirect method to here because we want it removed anytime the Setup Wizard is loaded.
I don't think it would happen in a normal flow, but this makes more sense in case some logic is changed in the future.
@@ -1166,7 +1166,7 @@ public function activate_sensei() { | |||
// Do not enable the wizard for sites that are created with the onboarding flow. | |||
if ( 'sensei' !== get_option( 'site_intent' ) ) { | |||
|
|||
set_transient( 'sensei_activation_redirect', 1, 30 ); | |||
update_option( 'sensei_activation_redirect', 1 ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to option, so if the user delays more than 30 seconds to click around, the Setup Wizard will still open in the relevant pages.
Test the previous changes of this PR with WordPress Playground. |
Test the previous changes of this PR with WordPress Playground. |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## trunk #7581 +/- ##
============================================
+ Coverage 51.76% 51.81% +0.04%
- Complexity 11323 11326 +3
============================================
Files 641 641
Lines 48185 48208 +23
Branches 470 470
============================================
+ Hits 24943 24978 +35
+ Misses 22861 22849 -12
Partials 381 381
Continue to review full report in Codecov by Sentry.
|
Test the previous changes of this PR with WordPress Playground. |
// Only redirects for admin users. | ||
|| ! current_user_can( 'manage_sensei' ) | ||
// Check if it's an admin screen that should redirect. | ||
|| ! $this->should_current_page_redirect_to_wizard() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now the redirect runs when the user accesses the pages we used to display the Setup Wizard notice (Dashboard, Plugins, and Sensei pages).
* | ||
* @return boolean | ||
*/ | ||
private function should_current_page_redirect_to_wizard() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I separated the checks for the notice and the redirect because the notice is very ugly in Sensei Home and we want the redirect if the user navigates to the Sensei Home.
Test the previous changes of this PR with WordPress Playground. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Just left some minor suggestions
/** | ||
* Testing if redirect option is cleared on setup wizard rendering. | ||
*/ | ||
public function testRenderWizardPageClearsRedirectOption() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we name the test function like we do for our other tests? Like this p6rkRX-35r-p2 (test**_When**_Does**)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure! I didn't do that to follow the old standard of this file, but to make it write I changed the whole file here now: 2d7b8eb
|
||
Sensei()->setup_wizard->render_wizard_page(); | ||
|
||
$this->assertFalse( get_option( 'sensei_activation_redirect', false ) ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can divide these in Arrange Act Assert sections?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -247,8 +267,6 @@ public function testActivationRedirectNoAdmin() { | |||
->method( 'redirect_to_setup_wizard' ); | |||
|
|||
$setup_wizard_mock->activation_redirect(); | |||
|
|||
$this->assertNotFalse( get_transient( 'sensei_activation_redirect' ), 'Transient should not be removed' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The next test still mentions Transient even though we are no longer using transients, should we rename?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed with the 2d7b8eb
|
||
set_transient( 'sensei_activation_redirect', 1, 30 ); | ||
update_option( 'sensei_activation_redirect', 1 ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using mock here, is it possible to test it using the Sensei_Test_Redirect_Helpers
trait? You can see an example here
sensei/tests/unit-tests/admin/home/tasks/task/test-class-sensei-home-task-pro-upsell.php
Lines 54 to 69 in 0132d5c
public function testMarkCompleteAndRedirect_WhenCalled_TriesToRedirectToRightPage() { | |
/* Arrange. */ | |
$this->login_as_admin(); | |
$this->prevent_wp_redirect(); | |
$redirect_location = ''; | |
/* Act. */ | |
try { | |
Sensei_Home_Task_Pro_Upsell::mark_completed_and_redirect(); | |
} catch ( Sensei_WP_Redirect_Exception $e ) { | |
$redirect_location = $e->getMessage(); | |
} | |
/* Assert. */ | |
$this->assertSame( 'https://senseilms.com/sensei-pro/?utm_source=plugin_sensei&utm_medium=upsell&utm_campaign=sensei-home', $redirect_location ); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure! Thank you for the suggestion! Updated here: 693851e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice! <3 Now they are covering much more realistically than the mock!
Test the previous changes of this PR with WordPress Playground. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! 🚀
Proposed Changes
There was a change on WP 6.5, where it doesn't reload the page when installing a plugin from the plugins directory through "Plugins > Add New". If the plugin is uploaded from a zip file the behavior continues the same as previously.
The Sensei Setup Wizard used to work immediately after we activate the plugin. With the recent change, our logic doesn't work anymore.
After a discussion (p1712321247992169-slack-C02NWDZBL0H), we decided to keep the intended behavior from WordPress core, and the Setup Wizard will only open after the user navigate in the WP admin.
Considered alternative
I thought of using this JS event https://github.com/WordPress/WordPress/blob/9bb63fd66503d139e0b3e8498c2a5a80cb3507a1/wp-admin/js/updates.js#L1123 and force a redirect when it's the Sensei activation, but it doesn't align with what WP core is proposing.
Testing Instructions
sensei_installed
from the database.Pre-Merge Checklist
Context